//
//  SendInquiryViewController.m
//  iWeddingDirectory
//
//  Created by Tung Ngo on 6/28/12.
//  Copyright (c) 2012 Home. All rights reserved.
//

#import "SendInquiryViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "API.h"
#import "User.h"

@implementation SendInquiryViewController

@synthesize textFieldName;
@synthesize textFieldEmail;
@synthesize textFieldSubject;
@synthesize textViewMessage;
@synthesize waitingIndicator;

@synthesize ids;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    waitingIndicator.hidden = YES;
    NSDictionary *loggedInUser = (NSDictionary *) [[NSUserDefaults standardUserDefaults] objectForKey:@"logged_in_user"];
    textFieldName.text = [loggedInUser objectForKey:@"nicename"];
    textFieldEmail.text = [loggedInUser objectForKey:@"email"];
    // Do any additional setup after loading the view from its nib.
    
    textViewMessage.layer.borderWidth = 1.0f;
    textViewMessage.layer.borderColor = [UIColor blackColor].CGColor;
}

- (void)viewDidUnload
{
    [self setTextViewMessage:nil];
    [self setTextFieldName:nil];
    [self setTextFieldEmail:nil];
    [self setWaitingIndicator:nil];
    [self setTextFieldSubject:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [textViewMessage release];
    [textFieldName release];
    [textFieldEmail release];
    [waitingIndicator release];
    [ids release];
    [textFieldSubject release];
    [super dealloc];
}

#pragma mark - Text Field Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark - Text View Delegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
    }
    return YES;
}

#pragma mark - Notification Handler

- (void)onSendInquiry:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kSendInquiryNotification object:nil];
    
    [waitingIndicator stopAnimating];
    [self.view setUserInteractionEnabled:YES];
    
    NSString *status = [notification.userInfo objectForKey:@"status"];
    if (status == nil || [status isEqualToString:@"error"]) {
        NSString *message = [notification.userInfo objectForKey:@"error"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    else if ([status isEqualToString:@"ok"]) {
        NSString *message = [notification.userInfo objectForKey:@"success"];
        if (message != nil) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            return;
        }
    }
}

#pragma mark - Button Clicked Handlers

- (IBAction)buttonSendClicked:(id)sender {
    NSString *name = textFieldName.text;
    NSString *email = textFieldEmail.text;
    NSString *subject = textFieldSubject.text;
    NSString *message = textViewMessage.text;
    
    NSString *msg = nil;
    if (name == nil || name.length == 0) {
        msg = @"Name is required";
    }
    else if (email == nil || email.length == 0) {
        msg = @"Email is required";
    }
    else if (subject == nil || subject.length == 0) {
        msg = @"Subject is required";
    }
    else if (message == nil || message.length == 0) {
        msg = @"Message is required";
    }
    if (msg != nil) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onSendInquiry:) name:kSendInquiryNotification object:nil];
    
    [waitingIndicator setHidesWhenStopped:YES];
    [waitingIndicator setHidden:NO];
    [waitingIndicator startAnimating];
    [self.view setUserInteractionEnabled:NO];
    
    API *api = [API sharedInstance];
    [api sendInquiry:ids email:email name:name subject:subject message:message];
}

@end
